package com.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.utils.JwtUtil;
import com.example.utils.LoginUtil;
import com.example.utils.RedisUtil;
import com.example.utils.common.AesCipherUtil;
import com.example.utils.common.Constant;
import com.example.utils.common.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author sword
 * @since 2021-03-26
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${shiro.config.refreshTokenExpireTime}")
    String refreshTokenExpireTime;
    @Resource
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String pwd = AesCipherUtil.enCrypto(user.getAccount()+user.getPassword());
        user.setPassword(pwd);
        User one = userService.getOne(new QueryWrapper<User>().setEntity(user));

        if (one!=null){
            String s1 = JSONObject.toJSONString(one);
            String token = LoginUtil.login(s1, refreshTokenExpireTime);
            return Result.success(token);
        }else {
            return Result.error("账号不存在或者密码错误");
        }

    }
    @GetMapping("/list")
    @RequiresPermissions(value = {"user:view"})
    public Result list(){
        return Result.success(userService.list());
    }
}
